package pack;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.ejb.EJB;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

import dao.BiletDAORemote;
import dao.EmployeeDAORemote;
import dto.BiletDTO;
import dto.EmployeeDTO;

@ManagedBean(name = "loginBean")
@RequestScoped
public class LoginBean implements Serializable {

	private static final long serialVersionUID = 1L;

	@EJB
	EmployeeDAORemote employeeDAO;

	@EJB
	BiletDAORemote biletDAO;

	@ManagedProperty(value = "#{employeeDTO}")
	private EmployeeDTO employeeDTO; // = new EmployeeDTO();

	@ManagedProperty(value = "#{biletDTO}")
	private BiletDTO bDTO;

	ExternalContext exCon;
	private int id;
	private String uname;
	private String upass;
	private String firstname;
	private String lastname;
	private String email;
	private Double salar;
	private String dateofbirth;
	private String teamleader;
	private String contactpersonde;
	private String areaManager;
	private int type;
	private String selected_language;

	private String localeCode;

	List<BiletDTO> bilete;

	private static Map<String, Object> countries;
	static {
		countries = new LinkedHashMap<String, Object>();
		countries.put("English", Locale.ENGLISH); // label, value
		countries.put("Germany", Locale.GERMAN);
	}

	public Map<String, Object> getCountriesInMap() {
		return countries;
	}

	public String getLocaleCode() {
		return localeCode;
	}

	public void setLocaleCode(String localeCode) {
		this.localeCode = localeCode;
	}

	public void countryLocaleCodeChanged(ValueChangeEvent e) {

		String newLocaleValue = e.getNewValue().toString();
		// loop country map to compare the locale code
		for (Map.Entry<String, Object> entry : countries.entrySet()) {
			if (entry.getValue().toString().equals(newLocaleValue)) {

				FacesContext.getCurrentInstance().getViewRoot()
						.setLocale((Locale) entry.getValue());

			}
		}
	}

	public String login() {
		employeeDTO = new EmployeeDTO();
		employeeDTO.setUsername(getUname());
		employeeDTO.setPassword(getUpass());

		FacesContext fc = FacesContext.getCurrentInstance();

		EmployeeDTO eDTO = employeeDAO
				.findEmployeeByUsernameAndPassword(employeeDTO);
		if (eDTO != null) {
			if (eDTO.getPassword() != null)
				if (eDTO.getUsername() != null) {
					setEmail(eDTO.getEmail());
					setDateofbirth(eDTO.getDateofbirth());
					setFirstname(eDTO.getFirstname());
					setLastname(eDTO.getLastname());
					setSalar(eDTO.getNet_salary());
					setTeamleader(eDTO.getTeamleaderro());
					setAreaManager(eDTO.getAreaManager());
					setContactpersonde(eDTO.getContactpersonde());
					setType(eDTO.getRank());
					setUname(eDTO.getUsername());
					setSelected_language(eDTO.getSelected_language());
					fc.getExternalContext().getSessionMap()
							.put("loggedInUser", eDTO);
					
					for (Map.Entry<String, Object> entry : countries.entrySet()) {
						if (entry.getValue().toString().equals(getSelected_language())) {

							FacesContext.getCurrentInstance().getViewRoot()
									.setLocale((Locale) entry.getValue());

						}
					}
					
					if (eDTO.getRank() == 1)
						return "administrator";
					if (eDTO.getRank() == 2)
						return "humanres";
					if (eDTO.getRank() == 3)
						return "teamleader";
					if (eDTO.getRank() == 4)
						return "secretary";
					if (eDTO.getRank() == 5) {

						return "employee";
					}
				}

		} else if (eDTO == null) {
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage(
					"Please enter a valid username and password"));
		}

		return "error";
	}

	public String inapoi() {
		// FacesContext context = FacesContext.getCurrentInstance();
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");
		if (eDTO != null) {

			setEmail(eDTO.getEmail());
			setDateofbirth(eDTO.getDateofbirth());
			setFirstname(eDTO.getFirstname());
			setLastname(eDTO.getLastname());
			setSalar(eDTO.getNet_salary());
			setTeamleader(eDTO.getTeamleaderro());
			setAreaManager(eDTO.getAreaManager());
			setContactpersonde(eDTO.getContactpersonde());
			setType(eDTO.getRank());
			setUname(eDTO.getUsername());
		} else
			System.out.println("Error: ");

		return "employee.xhtml";
	}

	public String inapoiTL() {
		// FacesContext context = FacesContext.getCurrentInstance();
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");
		if (eDTO != null) {

			setEmail(eDTO.getEmail());
			setDateofbirth(eDTO.getDateofbirth());
			setFirstname(eDTO.getFirstname());
			setLastname(eDTO.getLastname());
			setSalar(eDTO.getNet_salary());
			setTeamleader(eDTO.getTeamleaderro());
			setAreaManager(eDTO.getAreaManager());
			setContactpersonde(eDTO.getContactpersonde());
			setType(eDTO.getRank());
			setUname(eDTO.getUsername());
		} else
			System.out.println("Error: ");

		return "teamleader.xhtml";
	}

	public String inapoiA() {
		// FacesContext context = FacesContext.getCurrentInstance();
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");
		if (eDTO != null) {

			setEmail(eDTO.getEmail());
			setDateofbirth(eDTO.getDateofbirth());
			setFirstname(eDTO.getFirstname());
			setLastname(eDTO.getLastname());
			setSalar(eDTO.getNet_salary());
			setTeamleader(eDTO.getTeamleaderro());
			setAreaManager(eDTO.getAreaManager());
			setContactpersonde(eDTO.getContactpersonde());
			setType(eDTO.getRank());
			setUname(eDTO.getUsername());
		} else
			System.out.println("Error: ");

		return "administrator.xhtml";
	}

	public String inapoiHR() {
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");
		if (eDTO != null) {
			setEmail(eDTO.getEmail());
			setDateofbirth(eDTO.getDateofbirth());
			setFirstname(eDTO.getFirstname());
			setLastname(eDTO.getLastname());
			setSalar(eDTO.getNet_salary());
			setTeamleader(eDTO.getTeamleaderro());
			setAreaManager(eDTO.getAreaManager());
			setContactpersonde(eDTO.getContactpersonde());
			setType(eDTO.getRank());
			setUname(eDTO.getUsername());
		} else
			System.out.println("Error: ");
		return "humanres.xhtml";

	}

	public String inapoiS() {
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");
		if (eDTO != null) {
			setEmail(eDTO.getEmail());
			setDateofbirth(eDTO.getDateofbirth());
			setFirstname(eDTO.getFirstname());
			setLastname(eDTO.getLastname());
			setSalar(eDTO.getNet_salary());
			setTeamleader(eDTO.getTeamleaderro());
			setAreaManager(eDTO.getAreaManager());
			setContactpersonde(eDTO.getContactpersonde());
			setType(eDTO.getRank());
			setUname(eDTO.getUsername());
		} else
			System.out.println("Error: ");
		return "secretary.xhtml";

	}

	public void verify() {
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");

		ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc
				.getApplication().getNavigationHandler();

		if (eDTO != null) {
			if (eDTO.getRank() != 5) {
				nav.performNavigation("home.xhtml");
			}
		} else {
			nav.performNavigation("home.xhtml");
		}
	}

	public void verifyHR() {
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");

		ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc
				.getApplication().getNavigationHandler();

		if (eDTO != null) {
			if (eDTO.getRank() != 2) {
				nav.performNavigation("home.xhtml");
			}
		} else {
			nav.performNavigation("home.xhtml");
		}

	}

	public void verifyTL() {
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");

		ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc
				.getApplication().getNavigationHandler();

		if (eDTO != null) {
			if (eDTO.getRank() != 3) {
				nav.performNavigation("home.xhtml");
			}
		} else {
			nav.performNavigation("home.xhtml");
		}
	}

	public void verifyA() {
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");

		ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc
				.getApplication().getNavigationHandler();

		if (eDTO != null) {
			if (eDTO.getRank() != 1) {
				nav.performNavigation("home.xhtml");
			}
		} else {
			nav.performNavigation("home.xhtml");
		}
	}

	public void verifyS() {
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");

		ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc
				.getApplication().getNavigationHandler();

		if (eDTO != null) {
			if (eDTO.getRank() != 4) {
				nav.performNavigation("home.xhtml");
			}
		} else {
			nav.performNavigation("home.xhtml");
		}
	}

	public String biletDeVoie() {
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");
		setTeamleader(eDTO.getTeamleaderro());
		EmployeeDTO e = employeeDAO.findTLIdFromEmployee(eDTO);
		fc.getExternalContext().getSessionMap().put("tl_id", e);
		bilete = biletDAO.findAllTickets2(eDTO);
		if (bilete.size() != 0) {
			for (int i = 0; i < bilete.size(); i++) {
				bDTO = new BiletDTO();
				bDTO.setEmpl_id(bilete.get(i).getEmpl_id());
				bDTO.setEnd_time(bilete.get(i).getEnd_time());
				bDTO.setMotiv(bilete.get(i).getMotiv());
				bDTO.setStart_time(bilete.get(i).getStart_time());
				bDTO.setStatus(bilete.get(i).getStatus());
				bDTO.setTl_id(bilete.get(i).getTl_id());
				bDTO.setFirstname(bilete.get(i).getFirstname());
				bDTO.setLastname(bilete.get(i).getLastname());
				bDTO.setId(bilete.get(i).getId());
			}
		} else
			System.out.println("Nem jo!!!!!");

		return "biletdevoie.xhtml";
	}

	public String biletDeVoieTL() {
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");
		bilete = biletDAO.findAllTickets(eDTO);
		if (bilete.size() != 0) {
			for (int i = 0; i < bilete.size(); i++) {
				bDTO = new BiletDTO();
				bDTO.setEmpl_id(bilete.get(i).getEmpl_id());
				bDTO.setEnd_time(bilete.get(i).getEnd_time());
				bDTO.setMotiv(bilete.get(i).getMotiv());
				bDTO.setStart_time(bilete.get(i).getStart_time());
				bDTO.setStatus(bilete.get(i).getStatus());
				bDTO.setTl_id(bilete.get(i).getTl_id());
				bDTO.setFirstname(bilete.get(i).getFirstname());
				bDTO.setLastname(bilete.get(i).getLastname());
				bDTO.setId(bilete.get(i).getId());
			}
		} else
			System.out.println("Nem jo!!!!!");
		fc.getExternalContext().getSessionMap().put("bilete", bilete);
		return "bileteteamleader";
	}

	public String logOut() {
		FacesContext fc = FacesContext.getCurrentInstance();
		fc.getExternalContext().getSessionMap().clear();
		return "home.xhtml";
	}

	public List<BiletDTO> getBilete() {
		return bilete;
	}

	public void setBilete(List<BiletDTO> bilete) {
		this.bilete = bilete;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String getUpass() {
		return upass;
	}

	public void setUpass(String upass) {
		this.upass = upass;
	}

	public EmployeeDTO getEmployeeDTO() {
		return employeeDTO;
	}

	public void setEmployeeDTO(EmployeeDTO employeeDTO) {
		this.employeeDTO = employeeDTO;
	}

	public BiletDTO getbDTO() {
		return bDTO;
	}

	public void setbDTO(BiletDTO bDTO) {
		this.bDTO = bDTO;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Double getSalar() {
		return salar;
	}

	public void setSalar(Double salar) {
		this.salar = salar;
	}

	public String getDateofbirth() {
		return dateofbirth;
	}

	public void setDateofbirth(String dateofbirth) {
		this.dateofbirth = dateofbirth;
	}

	public String getTeamleader() {
		return teamleader;
	}

	public void setTeamleader(String teamleader) {
		this.teamleader = teamleader;
	}

	public String getContactpersonde() {
		return contactpersonde;
	}

	public void setContactpersonde(String contactpersonde) {
		this.contactpersonde = contactpersonde;
	}

	public String getAreaManager() {
		return areaManager;
	}

	public void setAreaManager(String areaManager) {
		this.areaManager = areaManager;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getSelected_language() {
		return selected_language;
	}

	public void setSelected_language(String selected_language) {
		this.selected_language = selected_language;
	}
}
